package com.jkoss.wine.customer.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.customer.entity.Shop;
import com.jkoss.wine.customer.service.IShopService;
import com.jkoss.wine.statistics.entity.MerchantOrder;
import com.jkoss.wine.statistics.service.IMerchantOrderService;

/**
 * 前端控制器
 * 
 * @Author Yellow
 * @Version 1.0, 2019-08-21
 * @See
 * @Since com.jkoss.wine.customer.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/customer/shopMerchantOrder")
public class ShopMerchantOrderController extends BaseController {

	@Autowired
	private IMerchantOrderService iMerchantOrderService;
	@Autowired
	private IShopService iShopService;

	@RequestMapping("/list")
	@RequiresPermissions("/customer/shopMerchantOrder/list")
	public String list(String orderNum, String sname, String saname, String addTime, HttpServletRequest request,
			HttpServletResponse response, ModelMap map, DwzPageBean dwzPageBean) {
		dwzPageBean.getCountResultMap().put("orderNum", orderNum);
		Wrapper wrapper = Condition.create().ne("o.del", 1).like("o.order_num", orderNum).like("s.shop_name", sname)
				.like("s.area_name", saname).like("o.add_time", addTime);
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("add_time", false);
		}
		Page<Shop> resultPage = iMerchantOrderService.selectShopVoByPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "customer/orders/list";
	}

	@RequestMapping("/toAudit")
	@RequiresPermissions("/customer/shopMerchantOrder/toAudit")
	public String toAudit(String id, ModelMap map) {
		MerchantOrder merchantOrder = iMerchantOrderService.selectById(id);
		String shopId = merchantOrder.getShopId();
		Shop shop = iShopService.selectById(shopId);
		map.put("shopName",shop.getShopName());
		map.put("record", merchantOrder);
		return "customer/orders/audit";

	}

	@RequestMapping("/audit")
	@RequiresPermissions("/customer/shopMerchantOrder/toAudit")
	@ResponseBody
	public Object audit(MerchantOrder merchantOrder) {
		merchantOrder.setUpdateTime(CommonUtil.getDate());
		merchantOrder.setRvid(getUserId());
		merchantOrder.setMerchantOperator(getUserId());
		if (iMerchantOrderService.updateById(merchantOrder)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	@RequestMapping("/delete")
	@RequiresPermissions("/customer/shopMerchantOrder/delete")
	@ResponseBody
	public Object delete(String[] id) {
		List list = new ArrayList();
		for (String str : id) {
			MerchantOrder merchantOrder = iMerchantOrderService.selectById(str);
			merchantOrder.setDel(1);
			list.add(merchantOrder);
		}
		if (iMerchantOrderService.updateBatchById(list)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

}
